package org.tourism.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tourism.VO.ComplaintVO;
import org.tourism.entity.Complaint;
import org.tourism.mapper.ComplaintMapper;
import org.tourism.service.ComplaintService;

import java.util.List;

@Service("complaintService")
public class ComplaintServiceImpl implements ComplaintService {
    @Autowired
    private ComplaintMapper complaintMapper;

    @Override
    public void addComplaint(ComplaintVO complaintVO) {
       Complaint complaint = complaintVO.toEntity();
       complaintMapper.insert(complaint);
    }

    @Override
    public int updateComplaint(Complaint complaint) {
        return complaintMapper.updateById(complaint);
    }

    @Override
    public int deleteComplaintById(int id) {
        return complaintMapper.deleteById(id);
    }

    @Override
    public List<ComplaintVO> getUnreviewedComplaints() {
        QueryWrapper<Complaint> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("review_status", 0);
        return ComplaintVO.toVOList(complaintMapper.selectList(queryWrapper));
    }

    @Override
    public List<ComplaintVO> getUnresolvedComplaints() {
        QueryWrapper<Complaint> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("process_status", false);
        return ComplaintVO.toVOList(complaintMapper.selectList(queryWrapper));
    }

    @Override
    public List<ComplaintVO> getComplaintsByUid(int uid) {
        QueryWrapper<Complaint> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        return ComplaintVO.toVOList(complaintMapper.selectList(queryWrapper));
    }

    @Override
    public Complaint getComplaintById(int id) {
        return complaintMapper.selectById(id);
    }

    @Override
    public void updateProcessStatusById(int id) {
        Complaint complaint = new Complaint();
        complaint.setId(id);
        complaint.setProcessStatus(true);
        complaintMapper.updateById(complaint);
    }

    @Override
    public void updateReviewStatusById(int id, int reviewStatus) {
        Complaint complaint = new Complaint();
        complaint.setId(id);
        complaint.setReviewStatus(reviewStatus);
        complaintMapper.updateById(complaint);
    }

    @Override
    public int getProcessStatusById(int id) {
        return getComplaintById(id).getReviewStatus();
    }
}
